package controller

import (
	"AiBus/global"
	"AiBus/model"
	"AiBus/request"
	"AiBus/service"
	"AiBus/utils"
	"fmt"
	"github.com/gin-gonic/gin"
)

// @Summary 添加待办
// @Success 200 {string} string json{"code","message"}
// @Param Authorization	header string true "Bearer xxxxxx"
// @Param data body request.ToDoReq{} true "添加待办参数"
// @Router /api/add_todo [post]
// @version 1.0
func ADDTODO(c *gin.Context) {
	req := request.ToDoReq{}
	UserID := c.GetString("open_id")
	if err := c.ShouldBindJSON(&req); err != nil {
		fmt.Println("req: ", req)
		utils.Fail(c, 5000, "请输入必传字段")
		return
	}
	service.ADDToDoList(c, req, UserID)
}

// @Summary 所有未完成待办加载
// @Success 200 {string} string json{"code","message"}
// @Param Authorization	header string true "Bearer xxxxxx"
// @Router /api/get_todo [get]
// @version 1.0
func GetToDo(c *gin.Context) {
	UserID := c.GetString("open_id")
	service.GetToDoList(c, UserID)
}

// @Summary 更新待办状态
// @Success 200 {string} string json{"code","message"}
// @Param Authorization	header string true "Bearer xxxxxx"
// @Param data body request.FinishToDoReq{} true "更新待办状态"
// @Router /api/finish [put]
// @version 1.0
func FinishToDo(c *gin.Context) {
	UserID := c.GetString("open_id")
	req := request.FinishToDoReq{}
	if err := c.ShouldBind(&req); err != nil {

		utils.Fail(c, 405, "参数传递错误")

	} else {
		service.FinishToDo(c, req, UserID)
	}
}

// @Summary 删除待办
// @Success 200 {string} string json{"code","message"}
// @Param Authorization	header string true "Bearer "
// @Param id query int true "待办id"
// @Router  /api/delete  [delete]
// @version 1.0
func DelToDo(c *gin.Context) {
	req := request.DelToDoReq{}

	if err := c.ShouldBind(&req); err != nil {

		utils.Fail(c, 405, "参数传递错误")

	} else {
		UserID := c.GetString("open_id")
		service.DeleTodo(c, req, UserID)
	}

}

// @Summary 加载所有已完成的待办
// @Success 200 {string} string json{"code","message"}
// @Param Authorization	header string true "Bearer xxxxxx"
// @Router /api/get_over_todo [get]
// @version 1.0
func GetOverToDo(c *gin.Context) {
	UserID := c.GetString("open_id")
	service.GetOverToDo(c, UserID)
}

// @Summary 加载所有用户取消的待办
// @Success 200 {string} string json{"code","message"}
// @Param Authorization	header string true "Bearer xxxxxx"
// @Router /api/get_del_todo [get]
// @version 1.0
func GetAllDelToDo(c *gin.Context) {
	UserID := c.GetString("open_id")
	service.GetDeleToDo(c, UserID)
}

/*待办状态*/
//func ToDoState(c *gin.Context) {
//	/*
//		0：表示正常
//		1：表示延期，如果延期则更新结束日期
//	*/
//	req := request.ToDoState{}
//	UserID := c.GetString("open_id")
//	if err := c.ShouldBind(&req); err != nil {
//		utils.Fail(c, 5000, "请输入必传字段")
//		return
//	}
//	service.ToDoState(c, req, UserID)
//}

// @Summary 待办分类加载
// @Param Authorization	header string true "Bearer xxxxxx"
// @Success 200 {string} string json{"code","message"}
// @Router /api/todo_class [get]
// @version 1.0
func ToDoClass(c *gin.Context) {
	var Count int64
	class := []model.ToDoClass{}
	tx := global.App.DB.Find(&class).Count(&Count)
	if tx.Error != nil || tx.RowsAffected == 0 {
		global.App.Log.Error(tx.Error.Error())
		//global.App.Lo
		utils.Fail(c, 4001, "待办分类获取失败")

	} else {
		tmp := []map[string]interface{}{}
		for _, item := range class {
			tmp = append(tmp, map[string]interface{}{
				"Id":    item.ID,
				"title": item.Title,
			})
		}
		utils.Success(c, Count, tmp)
	}
}

// @Summary 待办编辑
// @Param data body request.TODOUPDATEReq{} true "更新待办状态"
// @Param Authorization	header string true "Bearer xxxxxx"
// @Success 200 {string} string json{"code","message"}
// @Router /api/todo_update [PUT]
// @version 1.0
func ToDoUPDATE(c *gin.Context) {
	req := request.TODOUPDATEReq{}
	UserID := c.GetString("open_id")
	if err := c.ShouldBind(&req); err != nil {
		fmt.Println(req)
		utils.Fail(c, 5000, "请输入必传字段")
		return
	}
	service.ToDoUPDATE(c, req, UserID)

}

// @Summary 查询具体待办
// @Param id query int true "待办id"
// @Param Authorization	header string true "Bearer xxxxxx"
// @Success 200 {string} string json{"code","message"}
// @Router /api/todo_concreteness [PUT]
// @version 1.0
func ConcretenessTODO(c *gin.Context) {
	req := request.ConcretenessTODOReq{}
	UserID := c.GetString("open_id")
	if err := c.ShouldBind(&req); err != nil {
		utils.Fail(c, 5000, "请输入必传字段")
		return
	}
	service.Concreteness(c, req, UserID)
}
